package com.example.module.custom.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.module.custom.R;
import com.example.module.custom.R2;
import com.example.module.custom.adapter.PatrolPageAdapter;
import com.example.module.custom.bean.PQCDetailsBean;
import com.example.module.custom.http.HTTPURL;
import com.example.module.custom.ui.PQCAddActivity;
import com.example.module.custom.utils.SoftKeyboardUtil;
import com.example.module.custom.utils.SpacesItemDecoration;
import com.example.module.custom.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseFragment;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name BlockDemo
 * @class name：com.nand.module.function.fragment
 * @classdescribe 新增pqc检查项
 * @anthor ljp
 * @time 2020/1/7 10:55
 */
public class PQCTestFragment2 extends BaseFragment {
    private static int FEATURES;//功能选项（1、新增pqc，2、复查pqc 3、查看详情【不可操作】）
    @BindView(R2.id.recycler_view_test)
    RecyclerView recyclerViewTest;
    @BindView(R2.id.btn_save)
    Button btnSave;
    private PatrolPageAdapter mRecyclerAdapter;
    private List<PQCDetailsBean.DetailListBean> pqcDetailsBeans;
    private String oid;
    private String TAG = "PQCTestFragment2";
    private static PQCTestFragment2 pqcTestFragment = null;

    public static PQCTestFragment2 getInstance() {
        if (pqcTestFragment == null) {
            synchronized (PQCTestFragment2.class) {
                if (pqcTestFragment == null) {
                    pqcTestFragment = new PQCTestFragment2();
                }
            }
        }
        return pqcTestFragment;
    }

    public static PQCTestFragment2 newInstance(int flag, String oid) {
        Bundle bundle = new Bundle();
        PQCTestFragment2 fragment = new PQCTestFragment2();
        bundle.putInt("FEATURES", flag);
        bundle.putString("oid", oid);
        fragment.setArguments(bundle);
        pqcTestFragment = fragment;
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pqc_test;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        FEATURES = getArguments().getInt("FEATURES");
        oid = getArguments().getString("oid");
        pqcDetailsBeans = new ArrayList<>();
        if (FEATURES == 1 || FEATURES == 2) {
            btnSave.setVisibility(View.VISIBLE);
        }
        Log.i(TAG, "initView: ---FEATURES----" + FEATURES);
        recyclerViewTest.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerAdapter = new PatrolPageAdapter(getActivity(), pqcDetailsBeans, R.layout.item_pqc_test, FEATURES);
        recyclerViewTest.setAdapter(mRecyclerAdapter);
        recyclerViewTest.addItemDecoration(new SpacesItemDecoration(8));
        featuresJud(); //发起网络请求的方法
    }

    /**
     * @author ljp
     * @time 18:59
     * @describe 获得保存按钮
     */
    public Button getBtnSave() {
        return btnSave;
    }

    /**
     * @author ljp
     * @time 16:15
     * @describe 清空原始数据 重新添加
     */
    public void clearOldData() {
        pqcDetailsBeans.clear();
        mRecyclerAdapter.getPqcList().clear();
        mRecyclerAdapter.setData(pqcDetailsBeans);
        mRecyclerAdapter.notifyDataSetChanged();
        mRecyclerAdapter.clearEdit();
    }

    /**
     * @author ljp
     * @time 11:00
     * @describe 功能判断页面 //功能选项（1、新增pqc，2、复查pqc 3、查看详情【不可操作】）
     */
    private void featuresJud() {
        switch (FEATURES) {
            case 1:
                break;
            case 2:
                setHttpData(HTTPURL.PQC_DETAILS);
                break;
            case 3:
                btnSave.setVisibility(View.GONE);
                setHttpData(HTTPURL.PQC_DETAILS);
                break;
        }
    }

    public void setPqcDetailsBeans(PQCDetailsBean pqcDetailsBeanTest,int type) {
        // pqcDetailsBeanTest = PQCInforFragment.getPqcDetailsBeanTest();
        Log.i(TAG, "featuresJud: --pqcDetailsBeanTest----" + pqcDetailsBeanTest+"--type----"+type);
        if (pqcDetailsBeanTest != null)
            selectHttpData(pqcDetailsBeanTest,type);
    }

    /**
     * @author ljp
     * @time 16:05
     * @describe 网络请求（查询检查内容）
     */
    private void selectHttpData(final PQCDetailsBean pqcDetailsBean,int type) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("processOid", pqcDetailsBean.getProcessOid());
        httpParams.put("productionStage", pqcDetailsBean.getProductionStage());
        httpParams.put("materialOid", pqcDetailsBean.getMaterialOid());
        httpParams.put("checkType", type);
        OkGoUtils.getInstance().post(getActivity(), HTTPURL.PQC_ADD_CODE_SELECT_TEST, httpParams, 2, false, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                //Log.i(TAG, "onSuccess: -----"+response.getCode());
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    Log.i(TAG, "onSuccess: -----" + data);
                    pqcDetailsBeans = new Gson().fromJson(data, new TypeToken<List<PQCDetailsBean.DetailListBean>>() {
                    }.getType());
                    Log.i(TAG, "onSuccess: ---size----" + pqcDetailsBeans.size());
                   // if (pqcDetailsBeans != null && pqcDetailsBeans.size() != 0)
                        mRecyclerAdapter.setData(pqcDetailsBeans);
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });

    }
    /**
     * @author ljp
     * @time 16:05
     * @describe 网络请求 (查询详情)
     */
    private void setHttpData(String url) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("oid", oid);
        OkGoUtils.getInstance().post(getActivity(), url, httpParams, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                //Log.i(TAG, "onSuccess: -----"+response.getCode());
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    //  Log.i(TAG, "onSuccess: -----"+response.getDataInfo().toString());
                    PQCDetailsBean pqcDetailsBean = new Gson().fromJson(data, PQCDetailsBean.class);
                    if (pqcDetailsBean.getDetailList() != null || !pqcDetailsBean.getDetailList().isEmpty()) {
                        mRecyclerAdapter.setData(pqcDetailsBean.getDetailList());
                    }
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });

    }
    /**
     *  @author ljp
     *  @time   11:17
     *  @describe 保存按钮，将检验项值带到基本信息页面
     */
    @OnClick(R2.id.btn_save)
    public void onViewClicked() {
        SoftKeyboardUtil.hideKeyboard(getActivity());
        boolean isResult = false;
        boolean isQualified = true;
        List<PQCDetailsBean.DetailListBean> pqcList = mRecyclerAdapter.getPqcList();
        if (pqcList != null) {
            for (PQCDetailsBean.DetailListBean detailListBean : pqcList) {
                Log.i(TAG, "onViewClicked: ----" + detailListBean.toString());
                if (detailListBean.getResult() == 1) {
                    if (TextUtils.isEmpty(detailListBean.getUnqualiFiedDescribe())) {
                        ToastUtil.showShortToast("请输入不合格内容", getActivity());
                        return;
                    } else {
                        isResult = true;
                    }
                    isQualified = false;
                } else {
                    isResult = true;
                }
            }
            }
            if (isResult) {
                Gson gson = new Gson();
                String str = gson.toJson(pqcList);
                ((PQCAddActivity) getActivity()).setFragments(str, isQualified);
            }
    }
}
